主题
创建TCP客户端 - TcpClientCreate
函数简介
创建TCP客户端(基于回调的事件驱动模式)。
接口名称
TcpClientCreateDLL调用
c
int64_t TcpClientCreate(int64_t instance, TcpClientCallback callback, int64_t user_data, int32_t enable_packet_protocol);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| callback | TcpClientCallback | 事件回调函数。 |
| user_data | 长整数型 | 用户自定义数据,会在回调时传回。 |
| enable_packet_protocol | 整数型 | 是否启用消息分包协议:1=启用(推荐),0=禁用(原始模式)。 |
使用场景
| 场景 | enable_packet_protocol | 说明 |
|---|---|---|
| OLA 客户端互连 | 1 | 自动 4 字节长度前缀,解决粘包 |
| 对接第三方 TCP | 0 | 原始字节流,自行处理粘包 |
| Echo 客户端 | 1 | 回调 event_type=0 后发送,2 收包 |
完整 TCP 流程见 网络使用指南。
回调函数类型定义
c
void TcpClientCallback(int64_t client_handle, int32_t event_type, int64_t data, int32_t data_len, int64_t user_data);| 参数名 | 类型 | 说明 |
|---|---|---|
| client_handle | 长整数型 | 客户端句柄 |
| event_type | 整数型 | 事件类型(详见下方事件类型表) |
| data | 长整数型 | 数据指针(仅当 event_type=2 时有效) |
| data_len | 整数型 | 数据长度(仅当 event_type=2 时有效) |
| user_data | 长整数型 | 用户自定义数据,由创建客户端时传入 |
事件类型说明
| event_type | 事件名称 | 说明 |
|---|---|---|
| 0 | 连接成功 | 成功连接到服务器 |
| 1 | 连接失败 | 连接服务器失败 |
| 2 | 接收数据 | 接收到数据(data指向数据,data_len为长度) |
| 3 | 连接断开 | 与服务器的连接已断开 |
| 4 | 发送完成 | 数据发送完成 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long client = ola.TcpClientCreate(nullptr, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
// 回调中处理 event_type: 0连接成功 2收包 3断开
ola.TcpClientConnect(client, "127.0.0.1", 9000);
ola.TcpClientDestroy(client);
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0)
{
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000);
ola.TcpClientDestroy(client);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
client = ola.TcpClientCreate(None, 0, 1)
if client:
# enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000)
ola.TcpClientDestroy(client)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000);
ola.TcpClientDestroy(client);
}go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
long client = ola.TcpClientCreate(nil, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000)
ola.TcpClientDestroy(client);
}rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
long client = ola.TcpClientCreate(None, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.tcp_client_connect(&client, "127.0.0.1", 9000);
ola.TcpClientDestroy(client);
}cpp
var ola = com("OlaPlug.OlaSoft")
long client = ola.TcpClientCreate(0, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000)
ola.TcpClientDestroy(client);
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
client = ola.TcpClientCreate(0, 0, 1)
If client <> 0 Then
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000)
ola.TcpClientDestroy(client)
End Iftext
.局部变量 ola, OLAPlug
ola.创建 ()
client = ola.TcpClientCreate (0, 0, 1)
.如果真 (client ≠ 0)
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect (client, "127.0.0.1", 9000)
ola.TcpClientDestroy (client)
.如果真结束aardio
import OLAPlugServer;
var ola = OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000);
ola.TcpClientDestroy(client);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
long client = ola.TcpClientCreate(0, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000)
ola.TcpClientDestroy(client);
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long client = ola.TcpClientCreate(nullptr, 0, 1);
if (client != 0) {
// enable_packet_protocol=1 启用分包协议
ola.TcpClientConnect(client, "127.0.0.1", 9000);
ola.TcpClientDestroy(client);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long client = TcpClientCreate(instance, 0, instance, 1);
if (client != 0) {
// 注册 OnClientEvent 回调处理连接/收包
TcpClientConnect(instance, client, "127.0.0.1", 9000);
TcpClientDestroy(instance, client);
}csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long TcpClientCreate(long ola, long callback, long user_data, int enable_packet_protocol);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpClientDestroy(long ola, long client_handle);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpClientConnect(long ola, long client_handle, string host, int port);
long instance = CreateCOLAPlugInterFace();
long client = TcpClientCreate(instance, 0, instance, 1);
if (client != 0) {
// 注册 OnClientEvent 回调处理连接/收包
TcpClientConnect(instance, client, "127.0.0.1", 9000);
TcpClientDestroy(instance, client);
}python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
client = ola.TcpClientCreate(instance, 0, instance, 1)
if client:
// 注册 OnClientEvent 回调处理连接/收包
ola.TcpClientConnect(instance, client, "127.0.0.1", 9000);
ola.TcpClientDestroy(instance, client)返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 长整数型,返回客户端句柄,失败返回0。 |
注意事项
| 项目 | 说明 |
|---|---|
| 消息分包协议 | enable_packet_protocol=1:格式为 [4字节长度前缀(小端序)][消息体],自动解决 TCP 粘包,推荐用于自定义协议通信。 |
| 原始模式 | enable_packet_protocol=0:不添加任何协议头,可能出现粘包,适用于与第三方系统通信。 |
| 回调线程 | 回调函数在独立线程中执行,注意线程安全。 |
| 资源释放 | 使用完毕后需要调用 TcpClientDestroy 释放资源。 |
